import { Controller, Get, Post, Body, Patch, Param, Delete, HttpException, UseGuards, Req, ParseUUIDPipe, HttpStatus, Query, UsePipes, ParseIntPipe, } from '@nestjs/common';
import { ArticleService } from './article.service';
import { CreateArticleDto } from './dtos/create-article.dto';
import { UpdateArticleDto } from './dtos/update-article.dto';
import { ValidationPipe } from '../../pipes/validate.pipe'
import { AuthGuard } from '../../guard/auth.guard'
import { SearchDto } from '../../dtos/search.dto'

@Controller('article')
export class ArticleController {
  constructor(private readonly articleService: ArticleService) { }

  @Post()
  @UseGuards(AuthGuard)
  create(@Req() req, @Body(new ValidationPipe) createArticleDto: CreateArticleDto) {
    const userId = req.userId
    return this.articleService.create(userId, createArticleDto);

  }

  @Get()
  @UseGuards(AuthGuard)
  @UsePipes(new ValidationPipe())
  findAll(@Query() searchDto: SearchDto) {
    return this.articleService.findAll(searchDto);
  }

  @Get(':id')
  findOne(@Req() req, @Param('id', ParseUUIDPipe) id: string) {
    return this.articleService.findOne(req.userId, id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateArticleDto: UpdateArticleDto) {
    return this.articleService.update(+id, updateArticleDto);
  }

  @Delete(':id')
  remove(@Param('id') id: string) {
    return this.articleService.remove(+id);
  }

  @Post("audit")
  @UseGuards(AuthGuard)
  audiArticle(@Req() req, @Body('status', ParseIntPipe) status: number, @Body('article_id', ParseUUIDPipe) article_id: string) {
    const userId = req.userId
    if (status === undefined || article_id === undefined) {
      throw new HttpException("article_id和status是必填项", HttpStatus.BAD_REQUEST)
    }
    if (status != 1 && status != 2) {
      throw new HttpException("status只能是1,2", HttpStatus.BAD_REQUEST)
    }

    return this.articleService.auditArticle(userId, article_id, status)
  }
}
